package group2assignone.model;

import java.util.ArrayList;

/**
 * A Composite class that holds different ticket combinations.
 * @author group2
 */
public class TicketComposite extends TicketComponent {

    private ArrayList tickets = new ArrayList();
    private boolean booked = false;

    /**
     * Adds a ticket to the current ticket combination.
     * @param ticket
     */
    @Override
    public void addTicket(TicketComponent ticket) {
        tickets.add(ticket);
    }

    /**
     * Iterates through the ticket combination and books every ticket with 
     * the bookingNum and itemNumber.
     * Returns the itemNumber (item within the booking).
     * @param bookingNum
     * @param itemNumber
     * @return
     */
    public int bookTicket(int bookingNum, int itemNumber) {
        System.out.println("Composite of Tickets being booked");


        for (int i = 0; i < tickets.size(); i++) {
            TicketComponent comp = (TicketComponent) tickets.get(i);

            itemNumber = comp.bookTicket(bookingNum, itemNumber);
            booked = booked || comp.isBooked();
        }

        return itemNumber;
    }

    /**
     * Returns the total cost of the ticket.
     * @return
     */
    public double getCost() {
        double cost = 0;

        for (int i = 0; i < tickets.size(); i++) {
            TicketComponent comp = (TicketComponent) tickets.get(i);
            cost += comp.getCost();
        }

        return cost;
    }

    /**
     * returns a boolean indicating whether the flight indicated by the parameter
     * flightNum exists.
     * @param flightNum
     * @return
     */
    public boolean containsFlight(String flightNum) {
        boolean exists = false;

        for (int i = 0; i < tickets.size(); i++) {
            TicketComponent comp = (TicketComponent) tickets.get(i);
            exists = exists || comp.containsFlight(flightNum);
        }

        return exists;
    }

    /**
     * Returns a boolean which indicated whether the ticket was successfully booked.
     * @return
     */
    public boolean isBooked() {
        return booked;
    }
}
